package limousine.util;

import java.util.ArrayList;
import java.util.List;

public class PageIteratorFactory {

    public List<PageIterator> buildPages(int rowcount, int limit, String link) {
        int count = rowcount / limit;
        int mod = rowcount % limit;
        List<PageIterator> list = new ArrayList<PageIterator>();
        int num = 0;
        for (int i = 0; i < count; i++) {
            PageIterator p = new PageIterator((i+1),Integer.toString(num), link);
            num = (limit + num);            
            list.add(p);
        }
        if (mod > 0) {
            list.add(new PageIterator(list.size()+1,Integer.toString(num), link));
        }
        return list;
    }

    public List<PageIterator> reBuildPages(List<PageIterator> list,int currentOffset,int limit,int showCount) {
        List<PageIterator> newList = new ArrayList<PageIterator>();

        int n = Math.abs((currentOffset / limit) - showCount);
        if (currentOffset < (showCount * limit)) {
            n = 0;
        }
        int offsetStart = limit * n;
        int offsetEnd = currentOffset + (limit * showCount);

        for (int i = 0; i < list.size(); i++) {
            int xOffset = Integer.parseInt(list.get(i).getNumber());
            if ((xOffset >= offsetStart &&  xOffset <= offsetEnd) || xOffset == currentOffset) {                
                newList.add(list.get(i));
            }
        }

        return newList;
    }
    public static void main(String[] x) {

        PageIteratorFactory p = new PageIteratorFactory();

        int total = 26;
        int limit = 2;

        int currentOffset = 18;
        int showCount = 4;
        List<PageIterator> list = p.buildPages(total, 2, "");


        int n = Math.abs((currentOffset / limit) - showCount);
        if (currentOffset < (showCount * limit)) {
            n = 0;
        }
        int offsetStart = limit * n;
        int offsetEnd = currentOffset + (limit * showCount);

        for (int i = 0; i < list.size(); i++) {            
            int xOffset = Integer.parseInt(list.get(i).getNumber());

            if ((xOffset >= offsetStart &&  xOffset <= offsetEnd) || xOffset == currentOffset) {
                System.out.println("A=" + list.get(i).getNumber());
            }

        }


    }
}
